<?php

namespace App\VO\Product\MetaInfo;

use App\BO\Product\ProductBO;
use App\BO\PropertyAccessAndSetBO;
use App\Entities\ProductSkuEntity;
use App\Entities\ProductUnitEntity;

class ProductMetaInfoFactory
{
    /**
     * @param ProductBO $productBO
     * @return ProductInfo
     */
    public static function getProductInfo(ProductBO $productBO)
    {
        /** * @var ProductInfo $productInfo */
        $productInfo = PropertyAccessAndSetBO::sync($productBO, new ProductInfo());
        preg_match_all(
            '/src="(.*)"/U',
            $productBO->getDescription(),
            $matches
        );
        $productInfo->setDescriptionImages($matches[1] ?? []);
        return $productInfo;
    }

    /**
     * @param ProductSkuEntity $productSkuEntity
     * @return ProductSkuInfo
     */
    public static function getProductSkuInfo(ProductSkuEntity $productSkuEntity)
    {
        /** * @var ProductSkuInfo $productSkuInfo */
        $productSkuInfo = PropertyAccessAndSetBO::sync($productSkuEntity, new ProductSkuInfo());

        $productUnitEntity = ProductUnitEntity::find($productSkuEntity->getProductUnitId());
        if (!is_null($productUnitEntity)) {
            $productSkuInfo->setUnit($productUnitEntity->getUnitName());
        }
        return $productSkuInfo;
    }
}
